<?php

namespace app\api\controller;
use app\BaseController;

use app\model\OpenClass as OC;
class OpenClass extends ApiBase
{
    //list 
    public function list()
    {
        $list = (new OC())->where('status',1)->order('start_time','desc')->select()->toArray();
        $list = array_map(function($item){
            $item['start_time'] = date('Y-m-d H:i:s',$item['start_time']);
            $item['end_time'] = date('Y-m-d H:i:s',$item['end_time']);
            $item['is_free'] = $item['is_free'] == 1 ? '免费' : '收费';
            $item['course_imgs'] = json_decode($item['course_imgs'],true);
            $item['participants'] = explode("\n",$item['participants']);
            return $item;
        },$list);
        return $this->returnApi($list,code: 0);
    }

    //detail
    public function detail(){
        $id = input('id');
        $detail = (new OC())->where('id',$id)->find();
        if(!$detail){
            return $this->returnApi([],1,'课程不存在');
        }
        $detail['start_time'] = date('Y-m-d H:i:s',$detail['start_time']);
        $detail['end_time'] = date('Y-m-d H:i:s',$detail['end_time']);
        $detail['course_imgs'] = json_decode($detail['course_imgs'],true);
        $detail['participants'] = explode("\n",$detail['participants']);
        return $this->returnApi($detail,code: 0);
    }

}